// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
import user from './user'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state:{
        //小程序中本地存储 uni.get/setStorageSync()
        cartList: uni.getStorageSync('cartList') || [],
	},
    mutations:{
        changeCartList(state,payload){
            state.cartList = payload
            uni.setStorageSync('cartList',payload)
        }
    },
    getters:{
        //全选框的计算属性
        isAllselected(state){
            return state.cartList.every(item=>item.goods_select)
        },
        //被选中的所有商品
        selectedCartList(state){
            return state.cartList.filter(item=>item.goods_select)
        },
        //被选中的所有商品的总数
        selectedCartListCount(state,getters){
        return getters.selectedCartList.reduce((sum,item)=>{
                return sum += item.goods_count
        },0)
        },
        //被选中的所有商品的总价
        selectedCartListPrice(state,getters){
        return getters.selectedCartList.reduce((sum,item)=>{
                return sum += item.goods_count * item.goods_price
        },0)
        }
    },
    modules:{
        user
    }
})
export default store